/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuCadastrarExcluirEspecie.java
 *
 * Created on 02/12/2009, 13:54:42
 */
package JPaneis;

import Conector.Conector;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;
import siii.MenuPadrao;
/**
 *
 * @author Leo
 */
public class MenuCadastrarExcluirEspecie extends javax.swing.JPanel {

    private MenuPadrao menuPadrao;
    private Conector conector;
    private String objetivo;
    private ArrayList<String> lista = new ArrayList<String>();
    private String espAntiga;
    private boolean flagSelecionado = false;
    private ImageIcon iconeOK;
    private ImageIcon iconeERRO;
    private ImageIcon iconeATENCAO;

    /** Creates new form MenuCadastrarExcluirEspecie */
    public MenuCadastrarExcluirEspecie(MenuPadrao menu, Conector con, String obj) {
        this.menuPadrao = menu;
        this.objetivo = obj;
        this.conector = con;
        initComponents();
        if(this.objetivo.equals("C"))
            jButtonExcluir.setVisible(false);
        else
            jButtonSalvar.setText("Atualizar");
        this.setEspeciesJList();
        iconeOK = new ImageIcon(getClass().getResource("/Images/fish-48x48ok.png"));
        iconeERRO = new ImageIcon(getClass().getResource("/Images/fish-48x48erro.png"));
        iconeATENCAO = new ImageIcon(getClass().getResource("/Images/fish-48x48atencao1.png"));
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel13 = new javax.swing.JLabel();
        jLabelEspecie = new javax.swing.JLabel();
        jButtonSalvar = new javax.swing.JButton();
        jLabelComprarPeixePiscina = new javax.swing.JLabel();
        jButtonExcluir = new javax.swing.JButton();
        jTextFieldEspecie = new javax.swing.JTextField();
        jScrollPane1 = new javax.swing.JScrollPane();
        jListEspecies = new javax.swing.JList();
        jLabelEspecie1 = new javax.swing.JLabel();
        jButtonVoltar = new javax.swing.JButton();

        jLabel13.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/especies.png"))); // NOI18N

        jLabelEspecie.setText("Espécies Cadastradas");

        jButtonSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_add.png"))); // NOI18N
        jButtonSalvar.setText("Inserir");
        jButtonSalvar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonSalvarMouseClicked(evt);
            }
        });

        jLabelComprarPeixePiscina.setFont(new java.awt.Font("Tahoma", 0, 24));
        jLabelComprarPeixePiscina.setText("Cadastrar/Excluir Espécie");

        jButtonExcluir.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/peixe_cancelar.png"))); // NOI18N
        jButtonExcluir.setText("Excluir");
        jButtonExcluir.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonExcluirMouseClicked(evt);
            }
        });

        jListEspecies.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jListEspeciesMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(jListEspecies);

        jLabelEspecie1.setText("Nova Espécie");

        jButtonVoltar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/salvar.png"))); // NOI18N
        jButtonVoltar.setText("Ok");
        jButtonVoltar.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButtonVoltarMouseClicked(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(38, 38, 38)
                .addComponent(jLabel13, javax.swing.GroupLayout.PREFERRED_SIZE, 136, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(84, 84, 84)
                .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.PREFERRED_SIZE, 562, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(212, 212, 212))
            .addGroup(layout.createSequentialGroup()
                .addGap(179, 179, 179)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jButtonExcluir)
                                .addGap(251, 251, 251)
                                .addComponent(jButtonVoltar, javax.swing.GroupLayout.PREFERRED_SIZE, 111, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(jButtonSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabelEspecie1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(jTextFieldEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, 175, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addComponent(jLabelEspecie))
                .addContainerGap(209, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(14, Short.MAX_VALUE)
                .addComponent(jLabelComprarPeixePiscina, javax.swing.GroupLayout.PREFERRED_SIZE, 77, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jLabelEspecie)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabelEspecie1)
                            .addComponent(jTextFieldEspecie, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButtonSalvar)
                        .addGap(171, 171, 171)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jButtonExcluir)
                            .addComponent(jButtonVoltar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 279, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(78, 78, 78))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel13)
                .addContainerGap(347, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonSalvarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonSalvarMouseClicked

        if(this.objetivo.equals("C")) {
            String especieAdicionar = jTextFieldEspecie.getText().replace(" ", "");
            if(!especieAdicionar.replace(" ", "").equals("")) {
                String aux = especieAdicionar.substring(0, 1).toUpperCase() + especieAdicionar.substring(1).toLowerCase();
                especieAdicionar = aux;
                
                    if(conector.consultaEspecieCadastrada(especieAdicionar))
                        JOptionPane.showMessageDialog(this, "Espécie ja cadastrada no sistema", "AVISO", JOptionPane.WARNING_MESSAGE, iconeATENCAO);
                    else {
                        conector.cadastraEspecie(especieAdicionar);
                        JOptionPane.showMessageDialog(this, "Espécie cadastrada com sucesso", "AVISO", JOptionPane.WARNING_MESSAGE, iconeOK);
                        lista.add(especieAdicionar);
                    }
                    }
        else
            JOptionPane.showMessageDialog(this, "Favor preencher o nome da espécie", "AVISO", JOptionPane.WARNING_MESSAGE, iconeOK);
        }
        else {

            try {
                String especieEditar = jTextFieldEspecie.getText().replace(" ", "");
                if(!especieEditar.replace(" ", "").equals("")) {
                String aux = especieEditar.substring(0, 1).toUpperCase() + especieEditar.substring(1).toLowerCase();
                especieEditar = aux;
                if(conector.atualizarCadastroespecie(especieEditar, this.espAntiga) && flagSelecionado)
                    JOptionPane.showMessageDialog(this, "Cadastro atualizado com sucesso", "AVISO", JOptionPane.WARNING_MESSAGE, iconeOK);
                }
                else
                    JOptionPane.showMessageDialog(this, "Favor escolher uma espécie", "AVISO", JOptionPane.WARNING_MESSAGE, iconeOK);
                    this.cleanScreen();
                    this.setEspeciesJList();
            }
            catch(Exception e) {
                e.printStackTrace();
                conector = new Conector();
            }
        }
        flagSelecionado = false;
        this.cleanScreen();
        this.setEspeciesJList();
    }//GEN-LAST:event_jButtonSalvarMouseClicked

    private void jButtonExcluirMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonExcluirMouseClicked
        // TODO add your handling code here:
        if(conector.excluiEspecie(jListEspecies.getSelectedValue().toString())) {
            JOptionPane.showMessageDialog(this, "Espécie excluida com sucesso", "CONFIRMAÇÃO", JOptionPane.WARNING_MESSAGE, iconeOK);
            lista.remove(jListEspecies.getSelectedValue().toString());
        } else
            JOptionPane.showMessageDialog(this, "Ocorreu um erro na tentativa de excluir a espécie", "CONFIRMAÇÃO", JOptionPane.WARNING_MESSAGE, iconeERRO);
        this.setEspeciesJList();
        this.cleanScreen();
}//GEN-LAST:event_jButtonExcluirMouseClicked

    private void jListEspeciesMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jListEspeciesMouseClicked
        // TODO add your handling code here:
        if (jListEspecies.getModel().getSize() >= 0){
//            //pega da lista numero do servico.
            if(jListEspecies.getSelectedIndex() != -1) {
                String itens = lista.get(jListEspecies.getSelectedIndex());
                jTextFieldEspecie.setText((String) jListEspecies.getModel().getElementAt(jListEspecies.getSelectedIndex()));
            }
        }
        flagSelecionado = true;
        this.espAntiga = (String) jListEspecies.getModel().getElementAt(jListEspecies.getSelectedIndex());
}//GEN-LAST:event_jListEspeciesMouseClicked

    private void jButtonVoltarMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButtonVoltarMouseClicked
        // TODO add your handling code here:
        this.cleanScreen();
        this.menuPadrao.trocaPanel2(this.menuPadrao.getMenuPrincipal());
    }//GEN-LAST:event_jButtonVoltarMouseClicked

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonExcluir;
    private javax.swing.JButton jButtonSalvar;
    private javax.swing.JButton jButtonVoltar;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabelComprarPeixePiscina;
    private javax.swing.JLabel jLabelEspecie;
    private javax.swing.JLabel jLabelEspecie1;
    private javax.swing.JList jListEspecies;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextField jTextFieldEspecie;
    // End of variables declaration//GEN-END:variables

    public void cleanScreen() {
        jTextFieldEspecie.setText("");
    }

    public void setEspeciesJList(){
           

          //try retrieve data from file
             try {
//                 int i;
                 lista = conector.getEspecies();
//                 for(i = 1; i < lista.toArray().length; i++) {
//                     if(lista.toArray()[i-1].toString().compareTo(lista.toArray()[i].toString()) > 0) {
//                         String aux = lista.toArray()[i-1].toString();
//                         lista.toArray()[i-1] = lista.toArray()[i];
//                         lista.toArray()[i-1] = aux;
//                     }
//                 }
                 Collections.sort(lista);
                 System.out.println(lista);
                 jListEspecies.setListData(lista.toArray());
             }
             catch(Exception e)
             {
                e.printStackTrace();
             }
    }

}

